package com.cckj.projectapi.demo.controller;

import com.cckj.common.exception.BusinessException;
import com.cckj.common.response.ResultData;
import com.cckj.projectapi.demo.service.DemoService;
import com.cckj.common.event.BusinessEvent;
import com.cckj.common.event.EventPublisher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@Slf4j
@Validated
@RestController
public class Controller {

    @GetMapping("log")
    public ResultData ha(){
        log.trace("================{}=============","trace");
        log.debug("================{}=============","debug");
        log.info("================{}=============","info");
        log.warn("================{}=============","warn");
        log.error("================{}=============","error");
        return ResultData.ok();
    }

    @GetMapping("index")
    public String index(){
        String n = null;
        System.out.println(n.length());
        return "log";
    }


    @GetMapping("ex")
    public String ex() throws BusinessException {
        throw new BusinessException("哈哈哈哈");
    }


    @GetMapping("entity")
    public Entity entity(@Validated Entity entity)  {
       return entity;
    }


    @GetMapping("string")
    public String entity(String string)  {
        Assert.notNull(string,"入产错误");
        return "string";
    }

    @Autowired
    private EventPublisher eventPublisher;

    @GetMapping("event")
    public String event(String string)  {
        BusinessEvent businessEvent = new BusinessEvent();
        businessEvent.setMsg(string);
        eventPublisher.publishEvent(businessEvent);
        return "string";
    }


    @Autowired
    private RedisCacheManager cacheManager;

    @Autowired private DemoService demoService;

    @GetMapping("Cacheable")
    public Object Cacheable(String string)  {
        System.out.println(cacheManager);
        return demoService.cacheable(string);
    }

    @GetMapping("getCacheable")
    public Object getCacheable(String string)  {
        System.out.println(cacheManager);
        return demoService.getCacheable(string);
    }

    @GetMapping("cachePut")
    public Object cachePut(String string)  {
        System.out.println(cacheManager);
        return demoService.cachePut(string);
    }
    @GetMapping("cacheEvict")
    public Object cacheEvict(String string)  {
        System.out.println(cacheManager);
        return demoService.cacheEvict(string);
    }
    //concurrentMapCacheManager -> ConcurrentHashMap -> ConcurrentMapCache -> ConcurrentHashMap

//    @Autowired
//    private ThreadPoolTaskExecutor applicationTaskExecutor;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("redis")
    public Object redis(String string)  {
        redisTemplate.boundValueOps(string).set("StringValue");
        return "ok";
    }

    @GetMapping("asyn")
    public Object asyn(String string)  {
        demoService.asyn();
        return "demoService.cacheEvict(string)";
    }


}
